//
//  RideNaviViewController.m
//  ChatKit-OC
//
//  Created by th on 2017/4/4.
//  Copyright © 2017年 ElonChan. All rights reserved.
//

#import "RideNaviViewController.h"
#import "SpeechSynthesizer.h"
#import "MoreMenuView.h"

@interface RideNaviViewController ()<AMapNaviRideManagerDelegate, AMapNaviRideViewDelegate, MoreMenuViewDelegate>

@property (nonatomic, strong) MoreMenuView *moreMenu;

@end

@implementation RideNaviViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    [self initRideView];
    
    [self initRideManager];
    
    [self initMoreMenu];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBarHidden = YES;
    self.navigationController.toolbarHidden = YES;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    [self calculateRoute];
}

- (void)viewWillLayoutSubviews
{
    UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0)
    {
        interfaceOrientation = self.interfaceOrientation;
    }
    
    if (UIInterfaceOrientationIsPortrait(interfaceOrientation))
    {
        [self.rideView setIsLandscape:NO];
    }
    else if (UIInterfaceOrientationIsLandscape(interfaceOrientation))
    {
        [self.rideView setIsLandscape:YES];
    }
}

- (BOOL)prefersStatusBarHidden
{
    return YES;
}


#pragma mark - Initalization

- (void)initRideManager
{
    if (self.rideManager == nil)
    {
        self.rideManager = [[AMapNaviRideManager alloc] init];
        [self.rideManager setDelegate:self];
        
        [self.rideManager setAllowsBackgroundLocationUpdates:YES];
        [self.rideManager setPausesLocationUpdatesAutomatically:NO];
        
        //将driveView添加为导航数据的Representative，使其可以接收到导航诱导数据
        [self.rideManager addDataRepresentative:self.rideView];
    }
}


- (void)initRideView {
    if (self.rideView == nil) {
        self.rideView = [[AMapNaviRideView alloc] initWithFrame:self.view.bounds];
        self.rideView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        [self.rideView setDelegate:self];
        
        [self.view addSubview:self.rideView];
    }
}

- (void)initMoreMenu {
    
    if (self.moreMenu == nil) {
        self.moreMenu = [[MoreMenuView alloc] init];
        self.moreMenu.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        
        [self.moreMenu setDelegate:self];
    }
}


#pragma mark - Route Plan

- (void)calculateRoute
{
    //进行路径规划
    
    [self.rideManager calculateRideRouteWithStartPoint:self.startPoint endPoint:self.endPoint];
    
}

#pragma mark - AMapNaviDriveManager Delegate
/**
 *  发生错误时,会调用代理的此方法
 *
 *  @param error 错误信息
 */

- (void)rideManager:(AMapNaviRideManager *)rideManager error:(NSError *)error {
    [LCCKUtil showNotificationWithTitle:[error localizedDescription] subtitle:nil type:LCCKMessageNotificationTypeError];
}
/**
 *  骑行路径规划成功后的回调函数
 */

- (void)rideManagerOnCalculateRouteSuccess:(AMapNaviRideManager *)rideManager {
    //算路成功后开始GPS导航
    [self.rideManager startGPSNavi];
}

/**
 *  骑行路径规划失败后的回调函数
 *
 *  @param error 错误信息,error.code参照AMapNaviCalcRouteState
 */

- (void)rideManager:(AMapNaviRideManager *)rideManager onCalculateRouteFailure:(NSError *)error {
    [LCCKUtil showNotificationWithTitle:[error localizedDescription] subtitle:nil type:LCCKMessageNotificationTypeError];
}
/**
 *  导航播报信息回调函数
 *
 *  @param soundString 播报文字
 *  @param soundStringType 播报类型,参考AMapNaviSoundType
 */

- (void)rideManager:(AMapNaviRideManager *)rideManager playNaviSoundString:(NSString *)soundString soundStringType:(AMapNaviSoundType)soundStringType {
    [[SpeechSynthesizer sharedSpeechSynthesizer] speakString:soundString];
}


#pragma mark - AMapNaviWalkViewDelegate

- (void)rideViewCloseButtonClicked:(AMapNaviRideView *)rideView {
    
    //停止导航
    [self.rideManager stopNavi];
    [self.rideManager removeDataRepresentative:self.rideView];
    
    //停止语音
    [[SpeechSynthesizer sharedSpeechSynthesizer] stopSpeak];
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)rideViewMoreButtonClicked:(AMapNaviRideView *)rideView {
    //配置MoreMenu状态
    [self.moreMenu setTrackingMode:self.rideView.trackingMode];
    [self.moreMenu setShowNightType:self.rideView.showStandardNightType];
    
    [self.moreMenu setFrame:self.view.bounds];
    [self.view addSubview:self.moreMenu];
}


#pragma mark - MoreMenu Delegate

- (void)moreMenuViewFinishButtonClicked
{
    [self.moreMenu removeFromSuperview];
}

- (void)moreMenuViewNightTypeChangeTo:(BOOL)isShowNightType
{
    [self.rideView setShowStandardNightType:isShowNightType];
}

- (void)moreMenuViewTrackingModeChangeTo:(AMapNaviViewTrackingMode)trackingMode
{
    [self.rideView setTrackingMode:trackingMode];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
